package com.zhou.org.common;

import com.zhou.org.config.RightConfig;
import com.zhou.org.dao.Right;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

@Slf4j
@Configuration
public class RightUtil implements ApplicationRunner {
    private static RightConfig config;
    private static final Integer LOCK = 1;
    public RightUtil(RightConfig config) {
        RightUtil.config = config;
    }
    public static Map<String, Right> getRightMap(String rightKey, String rightType, int orgId){
        return config.getRightMap(rightKey, rightType, orgId);
    }
    public static List<Right> getRight(String rightKey, String rightType, int orgId){
        return config.getRight(rightKey, rightType, orgId);
    }

    public static void refreshMenuCache(){
        synchronized(LOCK){
            config.refreshRight();
        }
    }
    public static void refreshUserMenuCache(String rightKey, String rightType, int orgId){
        config.refreshUserMenuCache(rightKey, rightType, orgId);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        config.refreshRight();
    }
}
